package com.subway.servlet;

import com.subway.entity.SubWayInfo;
import com.subway.service.Impl.SubWayServiceimpl;
import com.subway.service.SubWayService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "SubwayServlet",urlPatterns = "/subway")
public class SubwayServlet extends HttpServlet {
    private SubWayService subWayService = new SubWayServiceimpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String opr = request.getParameter("opr");
        PrintWriter out = response.getWriter();
        if ("list".equals(opr)) {
            List<SubWayInfo> list = subWayService.list();
            request.setAttribute("subwayList", list);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        } else if ("del".equals(opr)) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            if (subWayService.del(id)) {
                response.sendRedirect("index.jsp");
            } else {
                out.print("<script tyoe='text/javascript'>alert('删除失败');location.href='index.jsp'</script>");
            }
        } else if ("getone".equals(opr)) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            SubWayInfo subWayInfo = subWayService.getSubWayInfoById(id);
            request.setAttribute("subwayInfo", subWayInfo);
            request.getRequestDispatcher("one.jsp").forward(request, response);
        } else if ("mgetone".equals(opr)) {
            Integer id = Integer.parseInt(request.getParameter("id"));
            SubWayInfo subWayInfo = subWayService.getSubWayInfoById(id);
            request.setAttribute("subwayInfo", subWayInfo);
            request.getRequestDispatcher("update.jsp").forward(request, response);
        } else if ("add".equals(opr)) {
            String subwayname = request.getParameter("subwayname");
            String startstation = request.getParameter("startstation");
            String endstation = request.getParameter("endstation");
            String starttime = request.getParameter("starttime");
            Integer stationnum = Integer.parseInt(request.getParameter("stationnum"));
            Integer price = Integer.parseInt(request.getParameter("price"));
            SubWayInfo subWayInfo = new SubWayInfo(0, subwayname, startstation, endstation, stationnum, starttime, price);
            if (subWayService.add(subWayInfo)) {
                out.print("<script type='text/javascript'>alert('添加成功');location.href='index.jsp'</script>");
            } else {
                out.print("<script type='text/javascript'>alert('添加失败');location.href='index.jsp'</script>");
            }
        } else if ("modify".equals(opr)) {
            System.out.println("你好111");
            String subwayname = request.getParameter("subwayname");
            String startstation = request.getParameter("startstation");
            String endstation = request.getParameter("endstation");
            String starttime = request.getParameter("starttime");
            Integer stationnum = Integer.parseInt(request.getParameter("stationnum"));
            Integer price = Integer.parseInt(request.getParameter("price"));
            Integer id = Integer.parseInt(request.getParameter("id"));

            SubWayInfo subWayInfo = new SubWayInfo(id, subwayname, startstation, endstation, stationnum, starttime, price);

            if (subWayService.modify(subWayInfo)) {
                out.print("<script type='text/javascript'>alert('修改成功');location.href='index.jsp'</script>");
            } else {
                out.print("<script type='text/javascript'>alert('修改失败');location.href='add.jsp'</script>");
            }
        }else if("check".equals(opr)){
            String subwayname = request.getParameter("subwayname");
            if(isChek(subwayname)){
                out.print("exists");
            }else {
                out.print("ok");
            }
        }
    }
    private boolean isChek(String subwayname){
        List<SubWayInfo> subWayInfoList = subWayService.list();
        List<String> listName = new ArrayList<>();
        for(SubWayInfo subWayInfo : subWayInfoList){
            listName.add(subWayInfo.getSubwayname());
        }
        return listName.contains(subwayname);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
